package cn.ubeta.b2c.api;

import cn.ubeta.b2c.bean.AddressInfo;
import cn.ubeta.b2c.bean.ApiResp;
import cn.ubeta.b2c.bean.CouponInfo;
import cn.ubeta.b2c.bean.FavoriteProductInfo;
import cn.ubeta.b2c.bean.FreightInfo;
import cn.ubeta.b2c.bean.JdPayParamInfo;
import cn.ubeta.b2c.bean.MeInfo;
import cn.ubeta.b2c.bean.OrderInfo;
import cn.ubeta.b2c.bean.UserInfo;
import cn.ubeta.b2c.utils.NetworkCallback;
import cn.ubeta.b2c.utils.NetworkHelper;

import org.apache.http.NameValuePair;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

import static cn.ubeta.b2c.utils.Constants.APPID;

/**
 * Created by denny on 16/9/5.
 */

public class ApiUser extends ApiBase<ApiUser.IUserBiz> {

    @Override
    public String getBasePath() {
        return "gw";
    }

    public static String PATH = new ApiUser().getBasePath();

    /**
     * 用户接口
     */
    interface IUserBiz  {
        @GET("getBaseInfo.do")
        Call<MeInfo> getMeInfo();

        @GET("getAddressInfo.do")
        Call<ApiResp<List<AddressInfo>>> getAddressList(@Query("page") int page);

        @POST("headerPic")
        Call<UserInfo> postUserHeaderPic();

        @GET("submitOrder.do")
        Call<ApiResp<OrderInfo>> submitOrder();

        @GET("getFreight.do")
        Call<FreightInfo> getFreight();

        @GET("payAccV2.do")
        Call<JdPayParamInfo> getJdPayParam(@Query("orderId") long orderId, @Query("orderInfo") String orderInfo, @Query("total") String total, @Query("goodsName") String goodsName);
    }

    /**
     * 获取我的页面展示信息
     * @param cb
     */
    public static void getMeInfo(Callback<MeInfo> cb) {
        MeInfo test = MeInfo.getTest();
        if (test == null) {
            //Response x = new Response<MeInfo>(null, test, null);
            cb.onResponse(null, null);
        } else {
            new ApiUser().create().getMeInfo().enqueue(cb);
        }
    }

    /**
     * 登录接口
     * @param phone
     * @param pswd
     * @param cb
     */
    public static void login(String phone, String pswd, NetworkCallback cb) {
        UserInfo test = UserInfo.getTest();
        if (test != null) {
            cb.onResponse(test);
        } else {
            String url = String.format("/login?phone=%s&pswd=%s", phone, pswd);
            NetworkHelper.call(PATH + url, cb);
        }
    }

    /**
     * 退出登录
     * @param cb
     */
    public static void logout(NetworkCallback<ApiResp> cb) {
        ApiResp test = ApiResp.getTest();
        if (test != null) {
            cb.onResponse(test);
        } else {
            NetworkHelper.call(PATH + "/logout", cb);
        }
    }

    /**
     * 获取优惠券信息
     * @param type
     * @param cb
     */
    public static void getCoupon(int type, int page, NetworkCallback<List<CouponInfo>> cb) {
        List<CouponInfo> test = CouponInfo.getTestData();
        if (test != null) {
            cb.onResponse(test);
        } else {
            String url = String.format("/coupon?type=%s&page=%d", type, page);
            NetworkHelper.call(PATH + url, cb);
        }
    }

    /**
     * 获取收藏商品列表
     * @param page
     * @param cb
     */
    public static void getFavoriteProduct(int page, NetworkCallback<List<FavoriteProductInfo>> cb) {
        List<FavoriteProductInfo> test = FavoriteProductInfo.getTestData();
        if (test != null) {
            cb.onResponse(test);
        } else {
            String url = String.format("/favorite?page=%d", page);
            NetworkHelper.call(PATH + url, cb);
        }
    }

    /**
     * 获取地址列表
     * @param page
     * @param cb
     */
    public static void getAddressList(int page, Callback<ApiResp<List<AddressInfo>>> cb) {
        List<AddressInfo> test = AddressInfo.getTestData();
        if (test != null) {
            cb.onResponse(null, null);
        } else {
            new ApiUser().create().getAddressList(page).enqueue(cb);
        }
    }

    /**
     * 上传用户头像
     * @param cb
     */
    public static void postUserHeaderPic(Callback cb) {
        new ApiUser().create().postUserHeaderPic().enqueue(cb);
    }

    /**
     * 提交订单
     * @param cb
     */
    public static void submitOrder(List<NameValuePair> params, Callback<ApiResp<OrderInfo>> cb) {
        new ApiUser().create(params).submitOrder().enqueue(cb);
    }

    /**
     * 获取运费
     */
    public static void getFreight(List<NameValuePair> params, Callback<FreightInfo> cb) {
        new ApiUser().create(params).getFreight().enqueue(cb);
    }
}
